package com.czjtxy.extraTerm.serverImpl;

import com.czjtxy.extraTerm.dao.TnoticeDao;
import com.czjtxy.extraTerm.daoImpl.TnoticeDaoImpl;
import com.czjtxy.extraTerm.entity.DO.Tnotice;
import com.czjtxy.extraTerm.entity.VO.TnoticeVO;
import com.czjtxy.extraTerm.server.NoticeService;
import com.czjtxy.extraTerm.util.Util;

import java.sql.SQLException;
import java.util.List;

/**
 * @author ZhangYing
 * @class 软件1903
 * @studentID 19852035
 * @date 2021/7/4
 */
public class NoticeServiceImpl implements NoticeService {
    TnoticeDao tnoticeDao = new TnoticeDaoImpl();

    @Override
    public List<TnoticeVO> getAll() throws SQLException, ClassNotFoundException {
        List<TnoticeVO> tnotices = null;
        try {
            tnotices = tnoticeDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tnoticeDao.closeResource();
        }
        return tnotices;
    }

    @Override
    public TnoticeVO getOne(int nid) throws SQLException {
        TnoticeVO tnoticeVO = new TnoticeVO();
        try {
            tnoticeVO = tnoticeDao.getOne(nid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tnoticeDao.closeResource();
        }
        return tnoticeVO;
    }

    @Override
    public void updateNotice(Tnotice tnotice) throws SQLException {
        try {
            tnoticeDao.updateNotice(tnotice);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tnoticeDao.closeResource();
        }
    }

    @Override
    public void deleteNotice(String ids) throws SQLException {
        String[] id = ids.split(",");
        try {
            for (String s : id) {
                tnoticeDao.deleteDept(Util.stringToInt(s));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tnoticeDao.closeResource();
        }
    }

    @Override
    public void addNotice(Tnotice tnotice) throws SQLException {
        try {
            tnoticeDao.addNotice(tnotice);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tnoticeDao.closeResource();
        }
    }

    @Override
    public List<TnoticeVO> getPart(Tnotice tnotice) throws SQLException {
        List<TnoticeVO> tnoticeVOS = null;
        try {
            tnoticeVOS = tnoticeDao.getPart(tnotice);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            tnoticeDao.closeResource();
        }
        return tnoticeVOS;
    }
}